<%= error_messages_for 'query' %>

<div class="box">
<div class="tabular">
<p><label for="query_name"><%=l(:field_name)%></label>
<%= text_field 'query', 'name', :size => 80 %></p>

<%# VVK start %>
<p>
  <label for="query_category"><%=l(:field_category)%></label>
  <%= text_field 'query', 'category', :size => "80" %>
</p>
<div id="query_categories_choices" class="autocomplete">
</div>
<%= javascript_tag "new Ajax.Autocompleter('query_category', 'query_categories_choices', '#{ url_for(:controller => 'smart_issues_sort', :action => 'autocomplete_for_category') }', { minChars: 1, frequency: 0.5, paramName: 'category' });" %>
<%# VVK end %>

<% if User.current.admin? || User.current.allowed_to?(:manage_public_queries, @project) %>
<p><label><%=l(:field_visible)%></label>
  <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PRIVATE %> <%= l(:label_visibility_private) %></label>
  <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_ROLES %> <%= l(:label_visibility_roles) %>:</label>
  <% Role.givable.sorted.each do |role| %>
    <label class="block role-visibility"><%= check_box_tag 'query[role_ids][]', role.id, @query.roles.include?(role), :id => nil %> <%= role.name %></label>
  <% end %>
  <label class="block"><%= radio_button 'query', 'visibility', Query::VISIBILITY_PUBLIC %> <%= l(:label_visibility_public) %></label>
  <%= hidden_field_tag 'query[role_ids][]', '' %>
</p>
<% end %>

<p><label for="query_is_for_all"><%=l(:field_is_for_all)%></label>
<%= check_box_tag 'query_is_for_all', 1, @query.project.nil?,
      :disabled => (!@query.new_record? && (@query.project.nil? || (@query.is_public? && !User.current.admin?))) %></p>

<p><label for="query_default_columns"><%=l(:label_default_columns)%></label>
<%= check_box_tag 'default_columns', 1, @query.has_default_columns?, :id => 'query_default_columns',
      :onclick => 'if (this.checked) {Element.hide("columns")} else {Element.show("columns")}' %></p>

<p><label for="query_group_by"><%= l(:field_group_by) %></label>
<%= select 'query', 'group_by', @query.groupable_columns.collect {|c| [c.caption, c.name.to_s]}, :include_blank => true %></p>
</div>

<fieldset><legend><%= l(:label_filter_plural) %></legend>
<%= render :partial => 'queries/filters', :locals => {:query => query}%>
</fieldset>

<fieldset><legend><%= l(:label_sort) %></legend>
<%# VVK start %>
  <p>
    &nbsp;&nbsp;
    <%= check_box 'query', 'sort_by_parent_first' %>
    <label for="query_sort_by_parent_first"><%=l(:label_qg_sort_by_parent_issue)%></label>
  </p>
<%# VVK end %>
<% 3.times do |i| %>
<%= i+1 %>:
<%= label_tag "query_sort_criteria_attribute_" + i.to_s,
              l(:description_query_sort_criteria_attribute), :class => "hidden-for-sighted" %>
<%= select_tag("query[sort_criteria][#{i}][]",
               options_for_select([[]] + query.available_columns.select(&:sortable?).collect {|column| [column.caption, column.name.to_s]}, @query.sort_criteria_key(i)),
               :id => "query_sort_criteria_attribute_" + i.to_s)%>
<%= label_tag "query_sort_criteria_direction_" + i.to_s,
              l(:description_query_sort_criteria_direction), :class => "hidden-for-sighted" %>
<%= select_tag("query[sort_criteria][#{i}][]",
                options_for_select([[], [l(:label_ascending), 'asc'], [l(:label_descending), 'desc']], @query.sort_criteria_order(i)),
                :id => "query_sort_criteria_direction_" + i.to_s) %>
<br />
<% end %>
</fieldset>

<%= content_tag 'fieldset', :id => 'columns', :style => (query.has_default_columns? ? 'display:none;' : nil) do %>
<legend><%= l(:field_column_names) %></legend>
<%= render :partial => 'queries/columns', :locals => {:query => query}%>
<% end %>

</div>

<%# VVK start %>
<% content_for :header_tags do %>
    <%= stylesheet_link_tag 'stylesheet', :plugin => SIS_AssetHelpers::PLUGIN_NAME %>
<% end %>
<%# VVK end %>
